package libaryinterface.login;
import libaryinterface.interfaceutils.PrivateInterface;
import libaryinterface.admin.AdminInterface;

import libaryinterface.user.UserInterface;
import pojo.User;
import service.BookRecordService;
import service.UserService;
import service.impl.BookRecordServiceImpl;
import service.impl.UserServiceImpl;
import utils.DateThread;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

//登录界面
public class LoginInterface {
    JFrame f1;
    JPanel p1;
    JButton b1,b3;
    JLabel l1, l2;
    JTextField t1;
    JPasswordField s1;
    UserService userService = new UserServiceImpl();
    BookRecordService bookRecordService = new BookRecordServiceImpl();
    public LoginInterface() {
        b1 = new JButton("返回初始界面");
        f1 = new JFrame("登录界面");
        p1 = new JPanel();
        l1 = new JLabel("学号：");
        t1 = new JTextField(23);
        l2 = new JLabel("密码：");
        s1 = new JPasswordField(23);
        b3 = new JButton("GO");
        ImageIcon bg = new ImageIcon("src//picture//ads2.jpg");
        JLabel label = new JLabel(bg);
        label.setSize(bg.getIconWidth(),bg.getIconHeight());
        f1.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        //2.把窗口面板设为内容面板并设为透明、流动布局。
        p1=(JPanel)f1.getContentPane();
        p1.setOpaque(false);
        p1.setLayout((new FlowLayout()));

        p1.setLayout(null);
        l1.setBounds(100, 150, 40, 30);
        t1.setBounds(140, 150, 200, 30);
        l2.setBounds(100, 200, 40, 30);
        s1.setBounds(140, 200, 200, 30);
        b3.setBounds(160, 300, 100, 50);
        b1.setBounds(50,450,100,30);
        b3.setBackground(Color.green);
        b3.setForeground(Color.white);
        l1.setForeground(Color.white);
        l2.setForeground(Color.white);
        b1.setFont(new Font("黑体",Font.PLAIN,10));
        b3.setFont(new Font("思源黑体",Font.PLAIN,20));
        p1.add(l1);
        p1.add(t1);
        p1.add(l2);
        p1.add(s1);
        p1.add(b3);
        p1.add(b1);
        f1.setVisible(true);
        f1.setBounds(600, 250, 415, 570);

        Thread dateThread = new DateThread();
        dateThread.start();

        //登录按钮监听器
        b3.addActionListener(new ActionListener() {
            Boolean flag;
            @Override
            public void actionPerformed(ActionEvent e) {
                String  account = t1.getText();
                String password = new String(s1.getPassword());
                User user = userService.login(account, password);
                if( user!=null){
                    if(user.getIsCaretaker() == 0){
                        new UserInterface(user.getAccount());
                        f1.dispose();
                    } else {
                        new AdminInterface();
                        f1.dispose();
                    }
                } else{
                    JOptionPane.showMessageDialog(null, "账号或密码错误", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new PrivateInterface();
                f1.dispose();
            }
        });
                f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}